<?php
define('LEGAL_OPER', true);
require('../../includes/admin-init.php');
require(ROOT_PATH . '/class/utils/FileUtil.class.php');
require(ADMIN_PATH . '/class/CommonData.class.php');
require(ADMIN_PATH . '/class/BlockData.class.php');

//频道页管理
if($method == 'staticcommon'){
	
	$pos =  to_int($_GET['pos']);
	if($pos > 0){
		$db = new DB();
		$db->beginTransaction();
		$channelTable = new BaseDao($db->getDb(), 'cms_channel','channel_id');
		$channel = $channelTable->getOne("channel_type=? and channel_pos=? and is_delete=?", array(2,$pos,0));
		if(isset($channel) && count($channel)>0 && $channel['tp_id']>0){
			$templateTable = new BaseDao($db->getDb(), 'cms_template','tp_id');
			$template = $templateTable->getOne("tp_id=? and is_delete=?", array($channel['tp_id'],0));
			if(isset($template) && count($template)>0){
				$blockData = new BlockData($db->getDb());
				$blockData->assignBlocks($channel['tp_id'], $smarty);
				$content='';
				try{
					$content = $smarty->fetch($template['tp_path']);
				}catch (Exception $e){
					echo 0;
					exit;
				}
				FileUtil::mkdirs(ROOT_PATH.'/template/common/');
				if($pos === 1){
					file_put_contents(ROOT_PATH.'/template/head.htm',$content);
					file_put_contents(ADMIN_PATH.'/template/head.htm',$content);
				}else if($pos === 2){
					file_put_contents(ROOT_PATH.'/template/foot.htm',$content);
					file_put_contents(ADMIN_PATH.'/template/foot.htm',$content);
				}
				echo 1;
				exit;
			}
		}
	}
	echo 0;		
	exit;
}


if($method == 'staticproduct'){
	$b = 1;
	$db = new DB();
	$db->beginTransaction();
	$productTable = new BaseDao($db->getDb(),'goods_product','pro_id');
	
	$catMap = CommonData::getCategoryMap($db->getDb());
	
	$products = $productTable->getAll("is_delete=?", array(0));
	foreach ($products as $product){
		//分类及属性名
		$cat = $catMap[$product['cat_id']];
		
		$parent = $catMap[$cat['parent_id']];
		$parent = $catMap[$parent['parent_id']];
		
		
		$catAttrs = CommonData::attrs2array($cat['attrs']);
		$product['attrs'] = $catAttrs;
		$product['cat'] = $cat;
		$product['parent'] = $parent;
		
		//描述
		$detailTable = new BaseDao($db->getDb(),'goods_product_detail','id');
		$detail = $detailTable->getOne('pro_id=? and type=?', array($product['pro_id'],1));
		if(count($detail)>0){
			$product['desc'] = $detail['content'];
		}
		//产品图片
		$imgTable = new BaseDao($db->getDb(),'goods_img','img_id');
		$sizeimg = $imgTable->getOne('img_type=? and pro_id=? and is_delete=?', array(2,$product['pro_id'],0));
		$imgs = $imgTable->getAll('img_type=? and pro_id=? and is_delete=?', array(1,$product['pro_id'],0),array('sort_order asc'));
		
		$product['sizeimg'] = $sizeimg;
		$product['imgs'] = $imgs;
		
		//属性
		$subProductTable = new BaseDao($db->getDb(),'goods_sub_product','spro_id');
		$oneAttrs = $subProductTable->getAll('pro_id=? and is_delete=?', array($product['pro_id'],0),array('sort_order asc'));
		
		$attrTable = new BaseDao($db->getDb(),'goods_product_attribute','attr_id');
		$twoAttrs = $attrTable->getAll('pro_id=? and is_delete=?', array($product['pro_id'],0),array('sort_order asc'));
		
		$product['attrone'] = $oneAttrs;
		$product['attrtwo'] = $twoAttrs;
		
		
		$subProductPhotoTable = new BaseDao($db->getDb(),'goods_sub_product_photo','photo_id');
		foreach ($oneAttrs as $subproduct){
			$subproduct['product'] = $product;
			//相框图片
			$photoarr = $subProductPhotoTable->getAll('spro_id=? and is_delete=?', array($subproduct['spro_id'],0),array('photo_pos asc'));
			$photos = array();
			foreach ($photoarr as $photo){
				$photos[$photo['photo_pos']][$photo['photo_type']] = $photo['photo_url'];
			}
			$subproduct['photos'] = $photos;
			try{
				$smarty->assign('subproduct',$subproduct);
				$content = $smarty->fetch('staticfile/product.htm');
				$str = '0000'.$subproduct['spro_id'];
				$htmldir = '/product/'.substr($str,(str_len($str)-4),2).'/'.substr($str,(str_len($str)-2),2).'/';
				FileUtil::mkdirs(ROOT_PATH.$htmldir);
				file_put_contents(ROOT_PATH.$htmldir.$subproduct['spro_id'].'.html',$content);
			}catch (Exception $e){
				//TODO:静态化失败
				$b = 0;
			}
		}
	}
	echo $b;
	exit;
}